<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="Da">
    
    <title>
        
            JavaFX小案例之简单截图工具 |
        
        Da的个人博客
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="/images/logo.svg">
    
<link rel="stylesheet" href="/css/font-awesome.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"da9527.gitee.io","root":"/","language":"zh-CN","path":"search.json"};
    KEEP.theme_config = {"toc":{"enable":true,"number":false,"expand_all":false,"init_open":false},"style":{"primary_color":"#CC0000","avatar":"/images/avatar.svg","favicon":"/images/logo.svg","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":true,"scale":true},"first_screen":{"enable":true,"background_img":"/images/bg.svg","description":"我不怕千万人阻挡,只怕自己投降"},"scroll":{"progress_bar":{"enable":false},"percent":{"enable":false}}},"local_search":{"enable":true,"preload":false},"code_copy":{"enable":true,"style":"default"},"pjax":{"enable":true},"lazyload":{"enable":true},"version":"3.4.2"};
    KEEP.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 月前","year":"%s 年前"};
  </script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
<div class="progress-bar-container">
    

    
        <span class="pjax-progress-bar"></span>
        <span class="pjax-progress-icon">
            <i class="fas fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            <a class="logo-title" href="/">
                Da的个人博客
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                首页
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                归档
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/about"
                            >
                                关于
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">首页</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">归档</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/about">关于</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">JavaFX小案例之简单截图工具</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="/images/avatar.svg">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">Da</span>
                        
                            <span class="author-label">Lv4</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;2021-05-23 20:51:31
    </span>
    
    

    
    
        <span class="article-wordcount article-meta-item">
            <i class="fas fa-file-word"></i>&nbsp;<span>1.4k 字</span>
        </span>
    
    
        <span class="article-min2read article-meta-item">
            <i class="fas fa-clock"></i>&nbsp;<span>6 分钟</span>
        </span>
    
    
        <span class="article-pv article-meta-item">
            <i class="fas fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <h1 id="JavaFX的第十五次接触"><a href="#JavaFX的第十五次接触" class="headerlink" title="JavaFX的第十五次接触"></a>JavaFX的第十五次接触</h1><blockquote>
<p> 自定义一个简单的截图工具</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.awt.Rectangle;</span><br><span class="line"><span class="keyword">import</span> java.awt.Robot;</span><br><span class="line"><span class="keyword">import</span> java.awt.image.BufferedImage;</span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.imageio.ImageIO;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javafx.application.Application;</span><br><span class="line"><span class="keyword">import</span> javafx.embed.swing.SwingFXUtils;</span><br><span class="line"><span class="keyword">import</span> javafx.geometry.Pos;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.Scene;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.Button;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.Label;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.TextField;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.Tooltip;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.image.ImageView;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.image.WritableImage;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.input.Clipboard;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.input.ClipboardContent;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.input.KeyCode;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.input.KeyCombination;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.input.Mnemonic;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.AnchorPane;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.Border;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.BorderStroke;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.BorderStrokeStyle;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.BorderWidths;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.HBox;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.paint.Paint;</span><br><span class="line"><span class="keyword">import</span> javafx.stage.Stage;</span><br><span class="line"><span class="keyword">import</span> javafx.stage.StageStyle;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> <span class="keyword">extends</span> <span class="title">Application</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">	<span class="keyword">private</span> ImageView iv;<span class="comment">// 显示图片的区域</span></span><br><span class="line">	<span class="keyword">private</span> Stage primary;<span class="comment">// 主窗口</span></span><br><span class="line">	<span class="keyword">private</span> Stage stage;<span class="comment">//截图页面窗口</span></span><br><span class="line">	<span class="keyword">private</span> HBox view;<span class="comment">//显示选中区域的面板</span></span><br><span class="line">	<span class="keyword">private</span> String saveSrc;<span class="comment">//截图默认保存位置</span></span><br><span class="line">	<span class="keyword">private</span> TextField text;<span class="comment">//输入框,用于获取图片的其他保存位置</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">double</span> start_SceneX;<span class="comment">//鼠标第一次点下的x坐标</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">double</span> start_SceneY;<span class="comment">//鼠标第一次点下的y坐标</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">double</span> end_SceneX;<span class="comment">//鼠标松开的x坐标</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">double</span> end_SceneY;<span class="comment">//鼠标松开的y坐标</span></span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		launch(args);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">(Stage stage)</span> <span class="keyword">throws</span> Exception</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		primary = stage;<span class="comment">// 指向当前窗口</span></span><br><span class="line"></span><br><span class="line">		Button button = <span class="keyword">new</span> Button(<span class="string">&quot;点我截图&quot;</span>);</span><br><span class="line">		Tooltip tooltip = <span class="keyword">new</span> Tooltip(<span class="string">&quot;可以自己修改位置,需要和输入框格式一致&quot;</span>);</span><br><span class="line">		text = <span class="keyword">new</span> TextField();<span class="comment">//截图保存位置</span></span><br><span class="line">		text.setPrefWidth(<span class="number">400</span>);</span><br><span class="line">		text.setPromptText(<span class="string">&quot;截图默认保存位置和名字为: D:/86181/Desktop/img.png&quot;</span>);</span><br><span class="line">		text.setTooltip(tooltip);	</span><br><span class="line">		</span><br><span class="line">		iv = <span class="keyword">new</span> ImageView();</span><br><span class="line">		iv.setFitWidth(<span class="number">700</span>);</span><br><span class="line">		iv.setFitHeight(<span class="number">400</span>);</span><br><span class="line"></span><br><span class="line">		AnchorPane ap = <span class="keyword">new</span> AnchorPane();</span><br><span class="line">		ap.setStyle(<span class="string">&quot;-fx-background-color: #567;&quot;</span>);</span><br><span class="line"><span class="comment">//		按钮位置</span></span><br><span class="line">		AnchorPane.setTopAnchor(button, <span class="number">30.0</span>);</span><br><span class="line">		AnchorPane.setLeftAnchor(button, <span class="number">50.0</span>);</span><br><span class="line"><span class="comment">//		输入框位置</span></span><br><span class="line">		AnchorPane.setTopAnchor(text, <span class="number">30.0</span>);</span><br><span class="line">		AnchorPane.setLeftAnchor(text, <span class="number">150.0</span>);</span><br><span class="line"><span class="comment">//		图片显示区域位置</span></span><br><span class="line">		AnchorPane.setTopAnchor(iv, <span class="number">100.0</span>);</span><br><span class="line">		AnchorPane.setLeftAnchor(iv, <span class="number">50.0</span>);</span><br><span class="line"></span><br><span class="line">		ap.getChildren().addAll(button,text,iv);</span><br><span class="line"></span><br><span class="line">		Scene scene = <span class="keyword">new</span> Scene(ap);</span><br><span class="line">		stage.setScene(scene);</span><br><span class="line">		stage.setTitle(<span class="string">&quot;截图&quot;</span>);</span><br><span class="line">		stage.setWidth(<span class="number">800</span>);</span><br><span class="line">		stage.setHeight(<span class="number">600</span>);</span><br><span class="line">		stage.show();</span><br><span class="line"></span><br><span class="line"><span class="comment">//		按钮点击事件</span></span><br><span class="line">		button.setOnAction(e -&gt; &#123;</span><br><span class="line">			</span><br><span class="line"><span class="comment">//			如果输入框不为空就改变截图保存位置</span></span><br><span class="line">			<span class="keyword">if</span> (!text.equals(<span class="string">&quot;&quot;</span>))</span><br><span class="line">			&#123;</span><br><span class="line">				saveSrc = text.getText();</span><br><span class="line">			&#125;<span class="keyword">else</span> </span><br><span class="line">			&#123;</span><br><span class="line">				saveSrc = <span class="string">&quot;D:/86181/Desktop/img.png&quot;</span>;</span><br><span class="line">			&#125;</span><br><span class="line"><span class="comment">//			调用截图页面方法</span></span><br><span class="line">			show();</span><br><span class="line">		&#125;);</span><br><span class="line"><span class="comment">//		绑定快捷键截图</span></span><br><span class="line">		KeyCombination key = KeyCombination.valueOf(<span class="string">&quot;ctrl+alt+p&quot;</span>);</span><br><span class="line">		Mnemonic mc = <span class="keyword">new</span> Mnemonic(button, key);</span><br><span class="line">		scene.addMnemonic(mc);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	显示截图页面</span></span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">()</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line"><span class="comment">//		显示截图页面先最小化主页面</span></span><br><span class="line">		primary.setIconified(<span class="keyword">true</span>);</span><br><span class="line"><span class="comment">//		新建截图窗口</span></span><br><span class="line">		stage = <span class="keyword">new</span> Stage();</span><br><span class="line"></span><br><span class="line">		AnchorPane ap = <span class="keyword">new</span> AnchorPane();</span><br><span class="line">		ap.setStyle(<span class="string">&quot;-fx-background-color: #b5b5b533&quot;</span>);<span class="comment">// 设置面板颜色,搞个朦胧的效果</span></span><br><span class="line"></span><br><span class="line">		Scene scene = <span class="keyword">new</span> Scene(ap);</span><br><span class="line">		scene.setFill(Paint.valueOf(<span class="string">&quot;#ffffff00&quot;</span>));</span><br><span class="line">		stage.setScene(scene);</span><br><span class="line">		stage.setFullScreen(<span class="keyword">true</span>);<span class="comment">// 设置全屏,盖满整个屏幕</span></span><br><span class="line">		stage.setFullScreenExitHint(<span class="string">&quot;&quot;</span>);<span class="comment">//把全屏默认的提示设置为空</span></span><br><span class="line">		stage.initStyle(StageStyle.TRANSPARENT);<span class="comment">// 设置场景透明</span></span><br><span class="line">		stage.show();</span><br><span class="line">		ap.setPrefWidth(stage.getWidth());</span><br><span class="line">		ap.setPrefHeight(stage.getHeight());</span><br><span class="line">		drag(ap);<span class="comment">//调用拖拽方法,获取截图的坐标位置、大小</span></span><br><span class="line"><span class="comment">//		重写ESC键的功能</span></span><br><span class="line">		scene.setOnKeyPressed(e -&gt; </span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span> (e.getCode() == KeyCode.ESCAPE)</span><br><span class="line">			&#123;</span><br><span class="line">				stage.close();<span class="comment">//关闭当前的截图页面</span></span><br><span class="line">				primary.setIconified(<span class="keyword">false</span>);<span class="comment">//把主页面显示出来</span></span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	拖拽方法,获取鼠标点下、拖动、松开的坐标</span></span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">drag</span><span class="params">(AnchorPane ap)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		</span><br><span class="line"><span class="comment">//		监听截图页面的鼠标点下事件,获取开始的坐标</span></span><br><span class="line">		ap.setOnMousePressed(e -&gt; </span><br><span class="line">		&#123;</span><br><span class="line"><span class="comment">//			获取起点坐标</span></span><br><span class="line">			start_SceneX = e.getScreenX();</span><br><span class="line">			start_SceneY = e.getScreenY();</span><br><span class="line">			</span><br><span class="line"><span class="comment">//			定义一个显示选中区域的面板</span></span><br><span class="line">			view = <span class="keyword">new</span> HBox();</span><br><span class="line">			view.setBackground(<span class="keyword">null</span>);<span class="comment">//设置背景为null</span></span><br><span class="line"><span class="comment">//			设置边框										颜色								线框风格		  圆角			边框厚度	</span></span><br><span class="line">			view.setBorder(<span class="keyword">new</span> Border(<span class="keyword">new</span> BorderStroke(Paint.valueOf(<span class="string">&quot;#f00&quot;</span>),BorderStrokeStyle.SOLID,<span class="keyword">null</span>,<span class="keyword">new</span> BorderWidths(<span class="number">2</span>))));</span><br><span class="line">			</span><br><span class="line"><span class="comment">//			设置选中区域的起点位置,注意X是距离左边的值,Y是距离上边的值</span></span><br><span class="line">			AnchorPane.setTopAnchor(view, start_SceneY);</span><br><span class="line">			AnchorPane.setLeftAnchor(view, start_SceneX);</span><br><span class="line">			ap.getChildren().add(view);<span class="comment">//放到截图页面</span></span><br><span class="line">			</span><br><span class="line">		&#125;);</span><br><span class="line">		</span><br><span class="line"><span class="comment">//		监听鼠标拖拽事件,不断获取新的坐标点</span></span><br><span class="line">		ap.setOnDragDetected(e -&gt;</span><br><span class="line">		&#123;</span><br><span class="line">			ap.startFullDrag();<span class="comment">//拖拽事件启动</span></span><br><span class="line">		&#125;);</span><br><span class="line">		</span><br><span class="line"><span class="comment">//		拖拽监听</span></span><br><span class="line">		ap.setOnMouseDragOver(e -&gt; </span><br><span class="line">		&#123;</span><br><span class="line">			</span><br><span class="line"><span class="comment">//			定义一个显示宽高的label标签</span></span><br><span class="line">			Label label = <span class="keyword">new</span> Label();</span><br><span class="line">			label.setAlignment(Pos.CENTER);<span class="comment">//对齐方式</span></span><br><span class="line"><span class="comment">//			设置宽高</span></span><br><span class="line">			label.setPrefWidth(<span class="number">170</span>);</span><br><span class="line">			label.setPrefHeight(<span class="number">30</span>);</span><br><span class="line">			ap.getChildren().add(label);<span class="comment">//添加label</span></span><br><span class="line"><span class="comment">//			label的位置</span></span><br><span class="line">			AnchorPane.setTopAnchor(label, start_SceneY - label.getPrefHeight());</span><br><span class="line">			AnchorPane.setLeftAnchor(label, start_SceneX);</span><br><span class="line">			label.setStyle(<span class="string">&quot;-fx-background-color: #567;&quot;</span> +</span><br><span class="line">						   <span class="string">&quot;-fx-text-fill: #fff;&quot;</span>);</span><br><span class="line">			</span><br><span class="line"><span class="comment">//			获取选中区域的面板的宽高</span></span><br><span class="line">			<span class="keyword">double</span> width = e.getSceneX() - start_SceneX;</span><br><span class="line">			<span class="keyword">double</span> height = e.getSceneY() - start_SceneY;</span><br><span class="line"><span class="comment">//			设置选中区域的面板</span></span><br><span class="line">			view.setPrefWidth(width);</span><br><span class="line">			view.setPrefHeight(height);</span><br><span class="line">			</span><br><span class="line"><span class="comment">//			设置label的文字</span></span><br><span class="line">			label.setText(<span class="string">&quot;当前宽度: &quot;</span> + (<span class="keyword">int</span>)width + <span class="string">&quot;,当前高度: &quot;</span> + (<span class="keyword">int</span>)height);</span><br><span class="line">			</span><br><span class="line">		&#125;);</span><br><span class="line">		</span><br><span class="line"><span class="comment">//		鼠标松开监听</span></span><br><span class="line">		ap.setOnMouseDragExited(e -&gt; </span><br><span class="line">		&#123;</span><br><span class="line"><span class="comment">//			获取终点坐标</span></span><br><span class="line">			end_SceneX = e.getSceneX();</span><br><span class="line">			end_SceneY = e.getSceneY();</span><br><span class="line">			</span><br><span class="line"><span class="comment">//			截图完成按钮</span></span><br><span class="line">			Button button = <span class="keyword">new</span> Button(<span class="string">&quot;完成&quot;</span>);</span><br><span class="line">			button.setPrefWidth(<span class="number">40</span>);</span><br><span class="line">			button.setPrefHeight(<span class="number">20</span>);</span><br><span class="line">			ap.setTopAnchor(button, end_SceneY);</span><br><span class="line">			ap.setLeftAnchor(button, end_SceneX - button.getPrefWidth());</span><br><span class="line"><span class="comment">//			取消按钮</span></span><br><span class="line">			Button button1 = <span class="keyword">new</span> Button(<span class="string">&quot;取消&quot;</span>);</span><br><span class="line">			button1.setPrefWidth(<span class="number">40</span>);</span><br><span class="line">			button1.setPrefHeight(<span class="number">20</span>);</span><br><span class="line">			ap.setTopAnchor(button1, end_SceneY);</span><br><span class="line">			ap.setLeftAnchor(button1, end_SceneX - button.getPrefWidth() - button1.getPrefWidth());</span><br><span class="line">			</span><br><span class="line">			ap.getChildren().addAll(button,button1);</span><br><span class="line">			</span><br><span class="line"><span class="comment">//			保存按钮监听,保存截图功能</span></span><br><span class="line">			button.setOnAction(ev -&gt; </span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">try</span></span><br><span class="line">				&#123;</span><br><span class="line">					getScreenImg();</span><br><span class="line">				&#125; <span class="keyword">catch</span> (Exception e1)</span><br><span class="line">				&#123;</span><br><span class="line">					<span class="comment">// TODO Auto-generated catch block</span></span><br><span class="line">					e1.printStackTrace();</span><br><span class="line">				&#125;<span class="comment">//调用获取图片方法</span></span><br><span class="line">			&#125;);</span><br><span class="line">			</span><br><span class="line"><span class="comment">//			取消按钮监听</span></span><br><span class="line">			button1.setOnAction(ev1 -&gt; </span><br><span class="line">			&#123;</span><br><span class="line">				stage.close();<span class="comment">//关闭当前窗口</span></span><br><span class="line">				primary.setIconified(<span class="keyword">false</span>);<span class="comment">//取消主页面的最小化</span></span><br><span class="line">			&#125;);</span><br><span class="line">			</span><br><span class="line">		&#125;);</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	获取屏幕截图</span></span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">getScreenImg</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		stage.close();<span class="comment">//关闭当前截图页面</span></span><br><span class="line">		</span><br><span class="line"><span class="comment">//		截图区域宽高</span></span><br><span class="line">		<span class="keyword">double</span> w = end_SceneX - start_SceneX;</span><br><span class="line">		<span class="keyword">double</span> h = end_SceneY - start_SceneY;</span><br><span class="line">		</span><br><span class="line"><span class="comment">//		要用awt的截图</span></span><br><span class="line">		Robot robot = <span class="keyword">new</span> Robot();</span><br><span class="line">		Rectangle rec = <span class="keyword">new</span> Rectangle((<span class="keyword">int</span>)start_SceneX, (<span class="keyword">int</span>)start_SceneY, (<span class="keyword">int</span>)w, (<span class="keyword">int</span>)h);</span><br><span class="line">		BufferedImage buffimg = robot.createScreenCapture(rec);</span><br><span class="line">		WritableImage wi = SwingFXUtils.toFXImage(buffimg, <span class="keyword">null</span>);</span><br><span class="line">		iv.setImage(wi);<span class="comment">//把截图数据保存到ImageView上</span></span><br><span class="line">		</span><br><span class="line"><span class="comment">//		把截图数据保存到剪切板</span></span><br><span class="line">		Clipboard cb = Clipboard.getSystemClipboard();<span class="comment">//获取系统剪切板</span></span><br><span class="line">		ClipboardContent content = <span class="keyword">new</span> ClipboardContent();</span><br><span class="line">		content.putImage(wi);<span class="comment">//保存图片数据</span></span><br><span class="line">		cb.setContent(content);<span class="comment">//保存图片数据到剪切板</span></span><br><span class="line">		</span><br><span class="line"><span class="comment">//		保存图片到桌面</span></span><br><span class="line">		<span class="keyword">if</span> (saveSrc.equals(<span class="string">&quot;&quot;</span>))<span class="comment">//输入框为空时设置默认路径</span></span><br><span class="line">		&#123;</span><br><span class="line">			saveSrc = <span class="string">&quot;D:/86181/Desktop/img.png&quot;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		ImageIO.write(buffimg, <span class="string">&quot;png&quot;</span>, <span class="keyword">new</span> File(saveSrc));</span><br><span class="line">		</span><br><span class="line"></span><br><span class="line">		primary.setIconified(<span class="keyword">false</span>);<span class="comment">//取消主窗口的最小化</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


        </div>

        
            <div class="post-copyright-info">
                <div class="article-copyright-info-container">
    <ul>
        <li>本文标题：JavaFX小案例之简单截图工具</li>
        <li>本文作者：Da</li>
        <li>创建时间：2021-05-23 20:51:31</li>
        <li>
            本文链接：https://da9527.gitee.io/2021/05/23/JavaFX小案例之简单截图工具/
        </li>
        <li>
            版权声明：本博客所有文章除特别声明外，均采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">BY-NC-SA</a> 许可协议。转载请注明出处！
        </li>
    </ul>
</div>

            </div>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/2021/05/29/JavaFX%E5%AD%A6%E4%B9%A0%E4%B9%8BTitledPane%E5%92%8CAccordion%E7%9A%84%E4%BD%BF%E7%94%A8/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">JavaFX学习之TitledPane和Accordion的使用</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/2021/05/23/JavaFX%E5%AD%A6%E4%B9%A0%E4%B9%8B%E8%8F%9C%E5%8D%95%E6%A0%8F%E8%A1%A5%E5%85%85/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">JavaFX学习之菜单栏补充</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2020</span>&nbsp;-&nbsp;
            
            2021&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">Da</a>
        </div>
        
            <script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    <span id="busuanzi_container_site_uv">
                        访问人数&nbsp;<span id="busuanzi_value_site_uv"></span>&ensp;
                    </span>
                
                
                    <span id="busuanzi_container_site_pv">
                        总访问量&nbsp;<span id="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        <div class="theme-info info-item">
            由 <a target="_blank" href="https://hexo.io">Hexo</a> 驱动&nbsp;|&nbsp;主题&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.4.2</a>
        </div>
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item page-aside-toggle">
                <i class="fas fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="fas fa-arrow-up"></i>
            </li>
        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
    </ul>
</div>

    </div>

    
        <aside class="page-aside">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#JavaFX%E7%9A%84%E7%AC%AC%E5%8D%81%E4%BA%94%E6%AC%A1%E6%8E%A5%E8%A7%A6"><span class="nav-text">JavaFX的第十五次接触</span></a></li></ol>
    </div>
</div>
        </aside>
    

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="搜索..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/header-shrink.js"></script>

<script src="/js/back2top.js"></script>

<script src="/js/dark-light-toggle.js"></script>



    
<script src="/js/local-search.js"></script>




    
<script src="/js/code-copy.js"></script>




    
<script src="/js/lazyload.js"></script>



<div class="post-scripts pjax">
    
        
<script src="/js/left-side-toggle.js"></script>

<script src="/js/libs/anime.min.js"></script>

<script src="/js/toc.js"></script>

    
</div>


    
<script src="/js/libs/pjax.min.js"></script>

<script>
    window.addEventListener('DOMContentLoaded', () => {
        const pjax = new Pjax({
            selectors: [
                'head title',
                '.page-container',
                '.pjax'
            ],
            history: true,
            debug: false,
            cacheBust: false,
            timeout: 0,
            analytics: false,
            currentUrlFullReload: false,
            scrollRestoration: false,
            // scrollTo: true,
        });

        document.addEventListener('pjax:send', () => {
            KEEP.utils.pjaxProgressBarStart();
        });

        document.addEventListener('pjax:complete', () => {
            KEEP.utils.pjaxProgressBarEnd();
            pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
            KEEP.refresh();
        });
    });
</script>



<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":120,"height":240},"mobile":{"show":false},"log":false});</script></body>
</html>
